package com.kaka.bus.engine.user.register;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.api.RetrofitUtils;
import com.kaka.bus.base.RxBaseActivity;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.util.U;

import butterknife.BindView;

/**
 * Created by zhangqi on 2016/12/1.
 */

public class RegisteraGreementActivity extends RxBaseActivity {


    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.webview_progressbar)
    ProgressBar mWebviewProgressbar;
    @BindView(R.id.webview)
    WebView mWebview;
    private String mUrl;

    @Override
    protected void initView() {
        mToolbarBack.setOnClickListener(v -> finish());
        Intent intent = getIntent();
        int type = intent.getIntExtra("type", -1);
        if (type == 1) {
            mToolbarName.setText("手机回收注册协议");
        }
        if (type == 2) {
            mToolbarName.setText("信息收集授权协议");
        }

        if (type == 3) {
            mToolbarName.setText("金狐贷");
            mUrl = intent.getStringExtra("url");
        }

        U.initWebView(mWebview, mWebviewProgressbar);
        initData(type);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_webview;
    }

    private void initData(int type) {
        if (type == 1) {
            addSubscription(RetrofitUtils.getInstance().build()
                    .getAgreement()
                    .compose(T.D())
                    .subscribeWith(new Result<HttpWrapper<String>>() {
                        @Override
                        protected void onSuccess(HttpWrapper<String> o) {
                            mWebview.loadData(o.getData(), "text/html; charset=UTF-8", null);
                        }
                    }));
        }

        if (type == 2) {
            addSubscription(RetrofitUtils.getInstance().build()
                    .juxinliInfo()
                    .compose(T.D())
                    .subscribeWith(new Result<HttpWrapper<String>>() {
                        @Override
                        protected void onSuccess(HttpWrapper<String> o) {
                            mWebview.loadData(o.getData(), "text/html; charset=UTF-8", null);
                        }
                    }));
        }

        if (type == 3) {
            mWebview.loadUrl(mUrl);
        }
    }
}
